

# jwt 返回的数据格式   自定义

def jwt_response_payload_handler(*args):

    print("jwt传入的参数：",args, type(args))
    return {
        'code': 200,
        "token": args[0],
        'user_id': args[1].pk,
        'username': args[1].username,
    }

# 重写authenticate方法
from django.contrib.auth.backends import ModelBackend
from .models import User
from django.contrib.auth.hashers import check_password

class lsAuth(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):

        user = User.objects.filter(username=username, is_staff=True, is_active=True).first()

        if user and check_password(password, user.password):
            # 验证通过
            return user

        else:
            return None



# 请求容联云 给用户手机发送短信验证码
from ronglian_sms_sdk import SmsSDK
from django.conf import settings
import json

def send_message(sms_code, mobile, expire=5):

    sms_sdk = SmsSDK(settings.ACCID, settings.AUTHTOKEN, settings.APPID)

    # 准备数据
    tid = '1'
    datas = ("%s"%sms_code, "%s"%expire)
    # 发送请求
    res = sms_sdk.sendMessage(tid=tid, mobile=mobile, datas=datas)

    #  res json字符串, 解析json数据
    res = json.loads(res)

    # 判断短信发送的结果
    if res.get("statusCode") == "000000":
        return sms_code

    else:
        return None







